﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace LotteryAdmin
{
    public partial class PlatformBank : UserControl
    {
        private LotteryAdminClient srv;
        public PlatformBank()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            this.Loaded += new RoutedEventHandler(PlatformBank_Loaded);
        }

        void PlatformBank_Loaded(object sender, RoutedEventArgs e)
        {
            LoadGrid();
        }
        void LoadGrid()
        {
            srv.GetCarrierBankAsync();
            loadingProgress.IsBusy = true;
            srv.GetCarrierBankCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                PagedCollectionView pageView = new PagedCollectionView(e.Result);
                dataPager.Source = pageView;
                this.dgList.ItemsSource = pageView;
            };
        }
        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dataPager.PageCount * dataPager.PageIndex+e.Row.GetIndex() + 1).ToString();
            (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            (this.dgList.Columns[6].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            if ((e.Row.DataContext as CarrierBankInfo).IsEnable == 0)
            {
                (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Content = "启用";
                (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Content = "关闭";
                (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Foreground = new SolidColorBrush(Colors.Red);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CarrierBankInfo model = (sender as Button).Tag as CarrierBankInfo;
            if (model.IsEnable == 0)
                model.IsEnable = 1;
            else
                model.IsEnable = 0;
            srv = WcfProxy.GetProxy;
            srv.ChangeBankEnableCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (sender as Button).Tag = model;
                    if (model.IsEnable == 0)
                    {
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                        (sender as Button).Content = "启用";
                    }
                    else
                    {
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                        (sender as Button).Content = "关闭";
                    }
                }
            };
            loadingProgress.IsBusy = true;
            srv.ChangeBankEnableAsync(model);
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ChangeBank cb_add = new ChangeBank();
            cb_add.ShowAddWindow();
            cb_add.Closed += (s, er) =>
            {
                LoadGrid();
            };
        }
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            ChangeBank cb_update = new ChangeBank();
            cb_update.ShowUpdateWindow((sender as Button).Tag as CarrierBankInfo);
            cb_update.Closed += (s, er) =>
            {
                LoadGrid();
            };
        }

        private void btnDelte_Click(object sender, RoutedEventArgs e)
        {
            LotteryAdminClient _srv = WcfProxy.GetProxy;
            _srv.DeletePlatformBankAsync((sender as Button).Tag as CarrierBankInfo);
            _srv.DeletePlatformBankCompleted += (sr, er) =>
            {
                if (er.Result)
                {
                    PagedCollectionView pageView = (PagedCollectionView)(dgList.ItemsSource);
                    System.Collections.ObjectModel.ObservableCollection<CarrierBankInfo> list
                        = (System.Collections.ObjectModel.ObservableCollection<CarrierBankInfo>)pageView.SourceCollection;
                    list.Remove((sender as Button).Tag as CarrierBankInfo);
                    MessageBox.Show("删除平台银行操作成功!");
                }
                else
                    MessageBox.Show("删除平台银行操作失败!");
            };
        }
    }
}
